package com.awan.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.awan.controller.UserItem;
import com.awan.model.ChatDatabaseHandler;
import com.awan.utils.Constanta;
import com.beem.project.beem.BeemApplication;
import com.beem.project.beem.R;
import com.beem.project.beem.ui.ContactList;

public class FriendsDataAdapter extends BaseAdapter{
	
	//private ImageLoader imageLoader;
	private SharedPreferences settings;
	//private SharedPreferences.Editor settingsEditor;
	private String currentJid;
	public static String[] privateFriend;
	private String[] privateStatus;
	public static String[] userAvatar;
	public static String[] userStatus;
	private ChatDatabaseHandler db;
	Activity mActivity;
	Context mContext;
    private LayoutInflater mInflater;
    ArrayList<UserItem> userItem;
    public FriendsDataAdapter(Activity act, Context c)
    {
    	settings = PreferenceManager.getDefaultSharedPreferences(c);
    	currentJid = settings.getString(BeemApplication.ACCOUNT_USERNAME_KEY, null).replace("@"+Constanta.chatDomain, "");	
    	mActivity=act;
           mContext=c;
           mInflater = LayoutInflater.from(c);
           db = new ChatDatabaseHandler(c);
           List<com.awan.controller.Chat> chatList = db.getCurrentChat();
           int recentSize = chatList.size();
           privateFriend = new String[recentSize];
           userAvatar = new String[recentSize];
           userStatus = new String[recentSize];
           privateStatus = new String[recentSize];
           userItem = ContactList.userList;
           
           if(recentSize>0){        	  
        	   int n =0;
	           for (com.awan.controller.Chat cn : chatList) {
	        	   String from = cn.getFrom();
	        	   if(!"Me".equalsIgnoreCase(from)){
	        		   /**
	        		    * sementara brooooooooooooooooooooooooooooooooooo
	        		    * */
	        		  for(int d=0;d<userItem.size();d++){
	        			   String tmpUserName = userItem.get(d).username;
	        			   if(tmpUserName.equals(from)){
	        				   String avatarUrl = Constanta.apiPath+"/"+userItem.get(d).avatar;
	        				   userAvatar[n] = avatarUrl;
	        			   }	        			   
	        		   }
	        		   privateFriend[n] = from;
	        		   n++;
	        	   }
	           }
           }
    }
    public int getCount(){    	
    	return privateFriend.length;
    }
    public Object getItem(int position)
    {
           return position;
    }
    public long getItemId(int position)
    {
           return position;
    }
    public View getView(final int position, View convertView, ViewGroup parent)
    {
    	
           ViewHolder holder=null;           
           if(convertView==null){
        	   holder = new ViewHolder();
           }else{
        	   	holder=null;
        	   	holder = new ViewHolder();    
           }
           
           convertView = mInflater.inflate(R.layout.private_friendlist, parent,false); 
		   holder.txtUserName=(TextView)convertView.findViewById(R.id.username);
		   holder.txtUserStatus=(TextView)convertView.findViewById(R.id.userstatus);
		   holder.userPresence=(ImageView)convertView.findViewById(R.id.userpresence);
		   holder.userThumb=(ImageView)convertView.findViewById(R.id.userthumb);
		   
		   String currentUser = privateFriend[position];
		   
		   currentUser = currentUser.replace("@"+Constanta.chatDomain, "");
		   
		   final Map<String, String> formParameters = new HashMap<String, String>();
		   formParameters.put("username", currentJid);
		   formParameters.put("targetname", currentUser);
		   
		   currentUser = currentUser.replace("@"+Constanta.chatDomain, "");
		   currentUser = currentUser.replace("/SMILES", "");
		   currentUser = currentUser.replace("/Beem", "");
		   
		   holder.txtUserName.setText(currentUser);
			
			if(userAvatar.length>=position){
				
				/*
				 * check
				 * */
				for(int d=0;d<userItem.size();d++){
     			   String tmpUserName = userItem.get(d).username;
     			   if(tmpUserName.equals(currentUser)){
     				   String avatarUrl = userItem.get(d).avatar;
     				   String _userStatus = userItem.get(d).status;
     				   userAvatar[position] = avatarUrl;
     				   userStatus[position] = _userStatus;
     			   }	        			   
     		   }
				
				holder.txtUserStatus.setText(userStatus[position]);
				
				 /** get image if already exists
				 * */
				//Log.e(getClass().getSimpleName(), "userAvatar[position]:"+userAvatar[position]);
				Bitmap bitmap = ContactList.imageLoader.getBitmapCache(userAvatar[position]);
				if(bitmap != null){
					holder.userThumb.setImageBitmap(bitmap);
				}
			}		
		   
		           
         
         if(privateStatus[position]=="1"){
        	 holder.userPresence.setImageResource(R.drawable.sign_online);
         }else{
        	 holder.userPresence.setImageResource(R.drawable.sign_offline);
         }          
         return convertView;
    }
    static class ViewHolder
    {        
    	ImageView userThumb;
        TextView txtUserName;        
        TextView txtUserStatus;
        ImageView userPresence;
    }

}
